﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using System.IO;
using System.Deployment.Application;

namespace D3StatsClient.Model
{
    class Sounds
    {
        private static SoundPlayer standard;
        private static SoundPlayer legendary;
        private static SoundPlayer rare;
        private static SoundPlayer crafting;
        private static SoundPlayer magic;

        public static string DefaultSoundLocation = AppDomain.CurrentDomain.BaseDirectory + "Sounds\\sound.wav";
        private static string DefaultSoundFolder = AppDomain.CurrentDomain.BaseDirectory + "Sounds\\";

        public static void InitSounds()
        {
            if (File.Exists(DefaultSoundLocation))
            {
                standard = new SoundPlayer(DefaultSoundLocation);
                standard.Load(); 
            }
            else
            {
                return;
            }

            if (File.Exists(Properties.Settings.Default.LegendarySoundFile))
            {
                legendary = new SoundPlayer(Properties.Settings.Default.LegendarySoundFile);
                legendary.Load();
            }
            else
                legendary = new SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + Properties.Settings.Default.LegendarySoundFile);

            if (File.Exists(Properties.Settings.Default.RareSoundFile))
            {
                rare = new SoundPlayer(Properties.Settings.Default.RareSoundFile);
                rare.Load();
            }
            else
                rare = new SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + Properties.Settings.Default.RareSoundFile);

            if (File.Exists(Properties.Settings.Default.CraftingSoundFile))
            {
                crafting = new SoundPlayer(Properties.Settings.Default.CraftingSoundFile);
                crafting.Load();
            }
            else
                crafting = new SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + Properties.Settings.Default.CraftingSoundFile); ;

            if (File.Exists(Properties.Settings.Default.MagicSoundFile))
            {
                magic = new SoundPlayer(Properties.Settings.Default.MagicSoundFile);
                magic.Load();
            }
            else
                magic = new SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + Properties.Settings.Default.MagicSoundFile); ;
        }

        private static void PlaySound(Sound sound)
        {
            switch (sound)
            {
                case Sound.None: 
                    break;
                case Sound.Legendary:
                    legendary.Stop();
                    legendary.Play(); 
                    break;
                case Sound.Rare:
                    rare.Stop();
                    rare.Play();
                    break;
                case Sound.Crafting:
                    crafting.Stop();
                    crafting.Play(); 
                    break;
                case Sound.Magic:
                    magic.Stop();
                    magic.Play();
                    break;
            }
        }

        public static void PlayNotificationSound(ItemQuality qLevel)
        {
            try
            {

                switch (qLevel)
                {
                    case ItemQuality.Magic1:
                    case ItemQuality.Magic2:
                    case ItemQuality.Magic3:

                        if (Properties.Settings.Default.PlayOnMagic)
                        {

                            Sounds.PlaySound(Sound.Magic);

                        }

                        break;


                    case ItemQuality.Legendary:

                        if (Properties.Settings.Default.PlayOnLegendary)
                        {
                            Sounds.PlaySound(Sound.Legendary);
                        }

                        break;
                    
                   
                    case ItemQuality.Rare4:
                    case ItemQuality.Rare5:
                    case ItemQuality.Rare6:

                        if (Properties.Settings.Default.PlayOnRare)
                        {
                            Sounds.PlaySound(Sound.Rare);
                        }

                        break;

                    case ItemQuality.Special:

                        if (Properties.Settings.Default.PlayOnCrafting)
                        {
                            Sounds.PlaySound(Sound.Crafting);
                        }

                        break;
                }
            }
            catch (Exception ex)
            {
                Logging.Logger.Log(string.Format("An error occured in PlayNotificationSound(). The error was {0}", ex.Message));
            }
        }
    }



    public enum Sound
    {
        None,
        Legendary,
        Rare,
        Crafting,
        Magic,
        Goblin
    }
}
